//
//  AutoScrollUILabel.m
//  Bus
//
//  Created by wangjiajia on 13-8-6.
//  Copyright (c) 2013年 wangjiajia. All rights reserved.
//

#import "AutoScrollUILabel.h"

@implementation AutoScrollUILabel{
    // float wordWidth;//文字长度，当文字比frame宽时，执行跑马灯
}



- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.*/

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    //float screenWidth  = self.frame.size.width;//该控件于父容器中满屏放置时的宽度即为屏幕宽度
    //float wordWidth=[self.text sizeWithFont:[UIFont fontWithName:@"Arial"size:20]].width;//计算字符宽度。宽度和字体样式应该没关系：粗体、倾斜、常规 都一样。
    
    /*
     app尺寸，去掉状态栏
     CGRect r = [ UIScreen mainScreen ].applicationFrame;
     r=0，20，320，460
     
     屏幕尺寸
     CGRect rx = [ UIScreen mainScreen ].bounds;
     r=0，0，320，480
     
     状态栏尺寸
     CGRect  rect = [[UIApplication sharedApplication] statusBarFrame];
     */
    float screenWidth=[UIScreen mainScreen].bounds.size.width;
    float wordWidth=self.bounds.size.width;//self.frame也行，大小一致 （外界设置了大小）
    
    // NSLog(@"没有滚动:screenWidth:%f,wordWidth:%f",screenWidth,wordWidth);

    if (wordWidth<=screenWidth) {
        return;
    }
    
    
    CGRect frame = self.frame;
    frame.origin.x = screenWidth;
    self.frame = frame;//起点
    //core animaiton
    [UIView beginAnimations:@"pao_ma_deng" context:nil];
    [UIView setAnimationDuration:10.0f * (wordWidth / screenWidth )];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationRepeatAutoreverses:NO];
    [UIView setAnimationRepeatCount: LONG_MAX];
    
    frame = self.frame;
    frame.origin.x = -wordWidth ;
    self.frame = frame;//终点
    [UIView commitAnimations];
    // NSLog(@"有滚动:screenWidth:%f,wordWidth:%f",screenWidth,wordWidth);
    
    //  NSLog(@"screen Frame %f,%f,%f,%f",self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
    
}


@end
